-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[

    变种官方的花【梅花、煤花、没花、莓花】
    梅花 - Plum blossom         plum_blossom
    煤花 - Coal flower           coal_flower
    没花 - No flower            no_flower   
    莓花 - Berry flower         berry_flower

]]--
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 参数表
    local flower_prefab = { "flower","planted_flower" }
    local ONE_DAY_TIME = 480 -- 一天有480秒
    local TEMPERATURE_SOURCE_RADIUS = 7 -- 温度源半径
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 其他辅助函数
    local function flower_kill_talk_sound(inst)
        inst.SoundEmitter:KillSound("talk")        
    end
    local function flower_say_by_index(inst,index)
        inst.components.hutao_com_dialogue:Say(index)
        inst.SoundEmitter:PlaySound("dontstarve/characters/woodie/lucytalk_LP", "talk")
        inst:DoTaskInTime(2.5,flower_kill_talk_sound)
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 自动转换任务
    local function find_firesuppressor(target,inst) --- 雪球机
        return target.prefab == "firesuppressor"
    end
    local function plum_blossom_auto_transform_update_task(inst)
        if TheWorld.state.israining then -- not inst:IsAsleep() and 
            --- 【梅花-花朵】在雨天会转变为【莓花-花朵】。
            inst.components.hutao_com_flower_switcher:Switch("berry_flower")
        elseif TheWorld.state.iswinter then
            --- 种植的【梅花-花朵】在冬天的环境内三天后，会自动转变为【没花-花朵】。
            local winter_auto_trans_timer = inst.components.hutao_com_flower_switcher:Add("winter_auto_trans_timer",1)
            if winter_auto_trans_timer >= 3 * ONE_DAY_TIME then
                inst.components.hutao_com_flower_switcher:Switch("no_flower")
            end
            --- 附近（半径7）有热源（篝火，营火，龙蝇火炉）或下雨或变潮湿会重置这一进程。
            if inst.components.hutao_com_flower_switcher:IsNearHotSource(TEMPERATURE_SOURCE_RADIUS)
                or inst:GetIsWet()
            then
                inst.components.hutao_com_flower_switcher:Set("winter_auto_trans_timer",0)
            end
        elseif TheWorld.state.issummer then
            --- 种植的【梅花-花朵】在夏天的环境内一天后，会自动转变为【煤花-花朵】。
            local summer_auto_trans_timer = inst.components.hutao_com_flower_switcher:Add("summer_auto_trans_timer",1)
            if summer_auto_trans_timer >= ONE_DAY_TIME then
                inst.components.hutao_com_flower_switcher:Switch("coal_flower")
            end
            --- 附近有冷源（吸热篝火、吸热营火、静谧冬灯（希儿专属））或被雪球机降温或下雨或变潮湿可重置这一进程。
            if inst.components.hutao_com_flower_switcher:IsNearColdSource(TEMPERATURE_SOURCE_RADIUS)
                or inst.components.hutao_com_flower_switcher:IsNearColdSource(TUNING.FIRE_DETECTOR_RANGE,find_firesuppressor)
                or inst:GetIsWet()
            then
                inst.components.hutao_com_flower_switcher:Set("summer_auto_trans_timer",0)
            end
        end
    end
    local function berry_flower_auto_transform_update_task(inst)
        --- 【莓花-花朵】在非雨天环境下一天后转变为【梅花-花朵】。
        if not TheWorld.state.israining then
            local auto_trans_timer = inst.components.hutao_com_flower_switcher:Add("auto_trans_timer",1)
            if auto_trans_timer >= ONE_DAY_TIME then
                inst.components.hutao_com_flower_switcher:Switch("plum_blossom")
            end
        else
            inst.components.hutao_com_flower_switcher:Set("auto_trans_timer",0)
        end        
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 参数组
    local hutao_com_flower_switcher = require("components/hutao_com_flower_switcher")
    -------------------------------------------------------------------------------------------------
    --- 梅花
        hutao_com_flower_switcher:SetTypeSwtichFn("plum_blossom",function(inst)
            inst.AnimState:SetBank("hutao_plants_plum_blossom_flower")
            inst.AnimState:SetBuild("hutao_plants_plum_blossom_flower")
            inst.AnimState:PlayAnimation("plum_blossom")
            inst.components.pickable:SetUp("hutao_item_plum_blossom_petal", 10)
            -- inst:SetPrefabName("hutao_plant_plum_blossom")
            -- inst:SetPrefabNameOverride("hutao_plant_plum_blossom")
            MakeHauntableChangePrefab(inst,"hutao_plant_no_flower")
            inst.components.hutao_com_flower_switcher:SetOveridePrefab("hutao_plant_plum_blossom")
            TheWorld.components.hutao_com_special_timer_for_theworld:AddTimer(inst,plum_blossom_auto_transform_update_task, 1)
        end)
    -------------------------------------------------------------------------------------------------
    --- 煤花
        --[[
            【煤花-花朵】直接采摘的话，25%会获得【梅花-花瓣】，75%会获得7个煤炭。
        ]]
        hutao_com_flower_switcher:SetTypeSwtichFn("coal_flower",function(inst)
            -- inst.AnimState:SetBank("hutao_plants_plum_blossom_flower")
            -- inst.AnimState:SetBuild("hutao_plants_plum_blossom_flower")
            -- inst.AnimState:PlayAnimation("coal_flower")            
            -- inst.components.pickable.Pick = function(self,picker,...)
            --     if not (self.canbepicked and self.caninteractwith) then
            --         return
            --     end
            --     if not (picker and picker:IsValid() and picker:HasTag("player") and picker.components.inventory) then
            --         return
            --     end
            --     if math.random(1000)/10 < 25 then
            --         local item = SpawnPrefab("hutao_item_plum_blossom_petal")
            --         picker.components.inventory:GiveItem(item)
            --     else
            --         local item = SpawnPrefab("charcoal")
            --         item.components.stackable.stacksize = 7
            --         picker.components.inventory:GiveItem(item)
            --     end
            --     self.inst:Remove()                
            -- end
            -- inst:SetPrefabName("hutao_plant_coal_flower")
            -- inst:SetPrefabNameOverride("hutao_plant_coal_flower")

            inst:Hide()
            inst:DoTaskInTime(0,function()                    
                local x,y,z = inst.Transform:GetWorldPosition()
                inst:Remove()
                SpawnPrefab("hutao_plant_coal_flower").Transform:SetPosition(x,y,z)
            end)

        end)
    -------------------------------------------------------------------------------------------------
    --- 没花
        --[[
           【没花-花朵】直接采摘的话，有75%概率获得1朵【梅花-花瓣】和1个噩梦燃料。有25%什么也得不到（有台词）。
        ]]
        hutao_com_flower_switcher:SetTypeSwtichFn("no_flower",function(inst)
            -- inst.AnimState:SetBank("hutao_plants_plum_blossom_flower")
            -- inst.AnimState:SetBuild("hutao_plants_plum_blossom_flower")
            -- inst.AnimState:PlayAnimation("no_flower")            
            -- inst.components.pickable.Pick = function(self,picker,...)
            --     if not (self.canbepicked and self.caninteractwith) then
            --         return
            --     end
            --     if not (picker and picker:IsValid() and picker:HasTag("player") and picker.components.inventory) then
            --         return
            --     end
            --     if math.random(1000)/10 < 75 then
            --         local item = SpawnPrefab("hutao_item_plum_blossom_petal")
            --         picker.components.inventory:GiveItem(item)
            --         local item = SpawnPrefab("nightmarefuel")
            --         picker.components.inventory:GiveItem(item)
            --     end
            --     self.inst:Remove()
            -- end
            -- inst:SetPrefabName("hutao_plant_no_flower")
            -- inst:SetPrefabNameOverride("hutao_plant_no_flower")
            inst:Hide()
            inst:DoTaskInTime(0,function()                    
                local x,y,z = inst.Transform:GetWorldPosition()
                inst:Remove()
                SpawnPrefab("hutao_plant_no_flower").Transform:SetPosition(x,y,z)
            end)
        end)
    -------------------------------------------------------------------------------------------------
    --- 莓花 【莓花-花朵】采摘时50%会获得7个多汁浆果，50%获得梅花。
        local function pollinated_fn(inst,_table)
            if inst.___pollinated_cd then
                return
            end
            inst.___pollinated_cd = inst:DoTaskInTime(TUNING.HUTAO_DEBUGGING_MODE and 20 or 2*60, function()
                inst.___pollinated_cd = nil
            end)
            flower_say_by_index(inst,"flower.berry_flower_pollinated")
        end
        hutao_com_flower_switcher:SetTypeSwtichFn("berry_flower",function(inst)
            inst.AnimState:SetBank("hutao_plants_plum_blossom_flower")
            inst.AnimState:SetBuild("hutao_plants_plum_blossom_flower")
            inst.AnimState:PlayAnimation("berry_flower")
            -- inst.components.pickable:SetUp("berries_juicy", 10)
            MakeHauntableChangePrefab(inst,"hutao_plant_no_flower")
            inst.components.hutao_com_flower_switcher:SetOveridePrefab("hutao_plant_berry_flower")
            TheWorld.components.hutao_com_special_timer_for_theworld:AddTimer(inst,berry_flower_auto_transform_update_task, 1)
            inst.components.pickable.Pick = function(self,picker,...)
                if not (self.canbepicked and self.caninteractwith) then
                    return
                end
                if not (picker and picker:IsValid() and picker:HasTag("player") and picker.components.inventory) then
                    return
                end
                if math.random(1000)/10 < 50 then
                    local item = SpawnPrefab("berries_juicy")
                    item.components.stackable.stacksize = 7
                    picker.components.inventory:GiveItem(item)
                else
                    local item = SpawnPrefab("hutao_item_plum_blossom_petal")
                    picker.components.inventory:GiveItem(item)
                end
                self.inst:Remove()
            end
            if math.random(1000)/10 < 25 and not inst:IsAsleep() then
                -- flower_say_by_index(inst,"flower.berry_flower_new_spawn")
                inst:DoTaskInTime(0.5,flower_say_by_index,"flower.berry_flower_new_spawn")
            end
            inst:ListenForEvent("hutao_event.pollinator.pollinated",pollinated_fn)
        end)
    -------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 冷源热源
    hutao_com_flower_switcher:AddHotSourcePrefab("campfire") -- 营火
    hutao_com_flower_switcher:AddHotSourcePrefab("firepit") -- 火坑
    hutao_com_flower_switcher:AddHotSourcePrefab("dragonflyfurnace") -- 龙鳞火炉
    hutao_com_flower_switcher:AddColdSourcePrefab("coldfire") -- 吸热营火
    hutao_com_flower_switcher:AddColdSourcePrefab("coldfirepit") -- 吸热火坑
    hutao_com_flower_switcher:AddColdSourcePrefab("silence_winter_lamp") -- 静谧冬灯（希儿专属）
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
for k, prefab in pairs(flower_prefab) do
    AddPrefabPostInit(
    prefab,
    function(inst)
        ---------------------------------------------------------
        --- 声音组件
            if inst.SoundEmitter == nil then
                inst.entity:AddSoundEmitter()
            end
        ---------------------------------------------------------
        --- 说话组件
            if inst.components.talker == nil then
                inst:AddComponent("talker")
                inst.components.talker.offset = Vector3(0,20,0)
                inst.components.talker.fontsize = 20
            end
        ---------------------------------------------------------
        if not TheWorld.ismastersim then
            return
        end
        ---------------------------------------------------------
        --- 花朵类型切换器
            if inst.components.hutao_com_flower_switcher == nil then
                inst:AddComponent("hutao_com_flower_switcher")
            end
        ---------------------------------------------------------
        --- 台词控制器
            if inst.components.hutao_com_dialogue == nil then
                inst:AddComponent("hutao_com_dialogue")
            end
        ---------------------------------------------------------

        ---------------------------------------------------------
    end)
end


